package com.msxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msxt.dto.AboutusEntity;
import com.msxt.mapper.AboutusDao;
import com.msxt.result.Query;
import com.msxt.service.AboutusService;
import com.msxt.utils.PageUtils;
import com.msxt.view.AboutusView;
import com.msxt.vo.AboutusVO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service("aboutusService")
public class AboutusServiceImpl extends ServiceImpl<AboutusDao, AboutusEntity> implements AboutusService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<AboutusEntity> page = this.page(
				new Query<AboutusEntity>(params).getPage(),
				new QueryWrapper<>()
		);
		return new PageUtils(page);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params, QueryWrapper<AboutusEntity> wrapper) {
		Page<AboutusView> page = new Query<AboutusView>(params).getPage();

		// 确保至少有一个有效条件（避免空WHERE）
		if (wrapper.isEmptyOfNormal()) {
			wrapper.isNotNull("id"); // 添加永真条件
		}

		// 查询总数
		Long total = baseMapper.selectCountByWrapper(wrapper);
		page.setTotal(total);

		if (total > 0) {
			// 查询分页数据
			page.setRecords(baseMapper.listView(page, wrapper));
		} else {
			page.setRecords(Collections.emptyList());
		}

		return new PageUtils(page);
	}

	@Override
	public List<AboutusVO> listVO(QueryWrapper<AboutusEntity> wrapper) {
		return baseMapper.listVO(wrapper);
	}

	@Override
	public AboutusVO selectVO(QueryWrapper<AboutusEntity> wrapper) {
		return baseMapper.selectVO(wrapper);
	}

	@Override
	public List<AboutusView> listView(QueryWrapper<AboutusEntity> wrapper) {
		return baseMapper.listView(wrapper);
	}

	@Override
	public AboutusView selectView(QueryWrapper<AboutusEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}
}